//
//  BuddyInfoManager.m
//  一点互助
//
//  Created by rimi on 16/9/6.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "BuddyInfoManager.h"
#import "ResponseManager.h"

static BuddyInfoManager *manager = nil;
@interface BuddyInfoManager ()
@property (strong, nonatomic) NSMutableArray *buddyList;
@end
@implementation BuddyInfoManager
+(instancetype)shareBuddyInfoManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[BuddyInfoManager alloc]init];
        manager.buddyList = [NSMutableArray array];
    });
    return manager;
}
-(void)getAllBuddyListFromServerCompletion:(void (^)(NSString *, NSArray *))compltionHandle{
    [ResponseManager startPostRequestWithURLString:kGetFriendsListURL parameters:nil completionHandler:^(FBResponse *response) {
        if (response.success) {
            [self.buddyList removeAllObjects];
            [self.buddyList addObjectsFromArray:response.resultValue];
            if (compltionHandle) {
                compltionHandle(nil,[self.buddyList copy]);
            }
        }else{
            if(compltionHandle) {
                compltionHandle(response.resultDesc,nil);
            }
        }
    }];
    
}
//从本地获取好友
-(NSArray *)getBuddyListFromLocal{
    if (self.buddyList.count == 0) {
        return nil;
    }else{
        return [self.buddyList copy];
    }
}

-(NSDictionary *)getBuddyInfoFromLocalWith:(NSString *)phoneNumber{
    for (NSDictionary *buddyInfo in self.buddyList) {
        if ([buddyInfo[@"mobile"] isEqualToString:phoneNumber]) {
            return buddyInfo;
        }
    }
    return nil;
}

@end
